package za.co.johanmynhardt.jee.producer;

import java.util.List;

import javax.annotation.ManagedBean;
import javax.enterprise.context.RequestScoped;
import javax.enterprise.inject.Disposes;
import javax.enterprise.inject.Produces;
import javax.inject.Inject;
import javax.inject.Named;
import javax.persistence.EntityManager;

import za.co.johanmynhardt.jee.repository.PostList;
import za.co.johanmynhardt.jee.repository.PostRepository;
import za.co.johanmynhardt.jee.entity.Post;

/**
 * @author johan.mynhardt@thumbtribe.mobi
 * @version 2011.0720
 */
public class PostListProducer {
	@Inject
	@PostRepository
	EntityManager postRepository;

	@SuppressWarnings({"unchecked"})
	@Produces
	@Named
	@RequestScoped
	public List<Post> getPosts() {
		return postRepository.createQuery("select p from Post p order by p.date desc").getResultList();
	}

	public @Produces @PostList PostListProducer retrievePostListProducer() {
		return this;
	}

	public void disposePostListProducer(@Disposes @PostList PostListProducer postListProducer) {
	}
}
